<?php
/**
 * User: @lixu
 * Date: 2019-05-29
 * Time: 14:44
 */

namespace Biz\module\Classes\wikiTab;

use Doraemon\pockets\config\IniConfig;
use Doraemon\tools\Curl;
use Doraemon\tools\Log;

class WikiTab
{
    const TAB_TYPE = null;
    const CONFIG_FILE = 'recommendation_api';
    const CONFIG_SELECT = 'wiki';
    const PAGE_SIZE = 20;

    protected static function getWikiIdList($params, $page = 1, $pageSize = self::PAGE_SIZE, $tabType = null)
    {

        try {
            $params['pagesize'] = $pageSize != self::PAGE_SIZE ? $pageSize : self::PAGE_SIZE;
            $params['page'] = $page;

            $params = http_build_query($params);

            $url = self::getUrl($tabType);

            $result = Curl::Request('GET', $url . '?' . $params);

            if($result['json']['code'] == 0) {
                $result['json']['data']['content'] =  $result['json']['data']['content'] ? : [];
                return [
                    'rows' => $result['json']['data']['content'],
                    'total' => $result['json']['data']['total'],
                    'is_over' => count($result['json']['data']['content']) < $pageSize ? 1 : 0
                ];
            }
        } catch (\Exception $e) {
            Log::error('wiki搜索服务出现异常', [
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
            return ['rows' => [], 'total' => 0, 'is_over' => 1];
        }

        return ['rows' => [], 'total' => 0, 'is_over' => 1];
    }

    private static function getUrl($tabType)
    {
        if (is_null($tabType)) {
            $tabType = static::TAB_TYPE;
        }
        $config = IniConfig::getConfigSelect(self::CONFIG_FILE, self::CONFIG_SELECT);
        return $config[$tabType];
    }
}